package com.fhz.module_main.ui.index

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.QuickAdapterHelper
import com.fhz.loginmvvm.callback.ApiSuccessResponse
import com.fhz.module_main.R
import com.fhz.module_main.adapter.*
import com.fhz.module_main.databinding.FragmentRecommendBinding
import com.fhz.module_main.entity.RecommendBanner
import com.fhz.module_main.entity.RecommendEntity
import com.fhz.module_main.entity.TextCard
import com.fhz.module_main.viewmodel.RecommendViewModel
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.zhpan.bannerview.BannerViewPager
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
/**
 * A simple [Fragment] subclass.
 * Use the [RecommendFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class RecommendFragment : Fragment() {

    private lateinit var binding: FragmentRecommendBinding
    val viewModel: RecommendViewModel by viewModels()
    private lateinit var adapter: RecommendAdapter
    private lateinit var recommendBannerAdapter: RecommendBannerAdapter
    private lateinit var recommendTextCardAdapter: RecommendTextCardAdapter
    private lateinit var recommendFollowCardAdapter: RecommendFollowCardAdapter
    private lateinit var recommendVideoSmallCardAdapter: RecommendVideoSmallCardAdapter
    private lateinit var recommendVideoSmallCardAdapter1: RecommendVideoSmallCardAdapter
    private lateinit var recycleSelectAdapter: RecycleSelectAdapter
    private lateinit var mViewPager: BannerViewPager<RecommendBanner>

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentRecommendBinding.inflate(inflater, container, false)

        //banner的实现
        //为什么只能用匿名的呢?
//        binding.bannerView.adapter = RecommendBannerAdapter()


        // 确定Item的改变不会影响RecyclerView的宽高
        binding.recommendRecyclerView.setHasFixedSize(true)
        binding.recommendRecyclerView.layoutManager = LinearLayoutManager(activity)
        var list = ArrayList<RecommendEntity.ItemX>()
        var list1 = ArrayList<RecommendEntity.Data>()
        var followCardList = ArrayList<RecommendEntity.Data>()
        var videoSmallCardList = ArrayList<RecommendEntity.Data>()
        var videoSmallCardList1 = ArrayList<RecommendEntity.Data>()
//        adapter = RecommendAdapter(list)

        //Adapter里有很多的ItemType

        recycleSelectAdapter = RecycleSelectAdapter()
        recommendTextCardAdapter = RecommendTextCardAdapter()
        recommendFollowCardAdapter = RecommendFollowCardAdapter()
        recommendVideoSmallCardAdapter = RecommendVideoSmallCardAdapter()
        recommendVideoSmallCardAdapter1 = RecommendVideoSmallCardAdapter()
        val helper = QuickAdapterHelper.Builder(recycleSelectAdapter)
//            // 设置尾部"加载更多"
//            .setTrailingLoadStateAdapter(……)
//        // 设置首部“加载更多”
//        .setLeadingLoadStateAdapter(……)
        // 设置 ConcatAdapter 的配置
//        .setConfig()
        .build()
        helper.addAfterAdapter(recommendTextCardAdapter)
        helper.addAfterAdapter(recommendFollowCardAdapter)
        helper.addAfterAdapter(recommendVideoSmallCardAdapter)
        helper.addAfterAdapter(recommendVideoSmallCardAdapter)
        helper.addAfterAdapter(recommendVideoSmallCardAdapter1)

        binding.recommendRecyclerView.adapter = helper.adapter

        binding.refreshLayout.setRefreshHeader(ClassicsHeader(activity))
        binding.refreshLayout.setRefreshFooter(ClassicsFooter(activity))
        binding.refreshLayout.setOnRefreshListener { refreshlayout ->
            refreshlayout.finishRefresh(2000 /*,false*/) //传入false表示刷新失败
        }
        binding.refreshLayout.setOnLoadMoreListener(OnLoadMoreListener { refreshlayout ->
            refreshlayout.finishLoadMore(2000 /*,false*/) //传入false表示加载失败
        })


        viewModel.getRecommend().observe(viewLifecycleOwner){
//            println(it)
            val apiSuccessResponse = it as ApiSuccessResponse
//            list.addAll(apiSuccessResponse.body.itemList)
            val itemList = apiSuccessResponse.body.itemList[0].data.itemList
            val item = apiSuccessResponse.body.itemList[1].data.text
            println("-----$item")
            list.addAll(itemList)
            list1.add(apiSuccessResponse.body.itemList[1].data)
            followCardList.add(apiSuccessResponse.body.itemList[2].data)
            videoSmallCardList.add(apiSuccessResponse.body.itemList[3].data)
            videoSmallCardList1.add(apiSuccessResponse.body.itemList[4].data)
//            println(apiSuccessResponse.body.itemList[3].data.author.icon)
            recycleSelectAdapter.submitList(list)
            recommendTextCardAdapter.submitList(list1)
            recommendFollowCardAdapter.submitList(followCardList)
            recommendVideoSmallCardAdapter.submitList(videoSmallCardList)
            recommendVideoSmallCardAdapter1.submitList(videoSmallCardList1)
            recycleSelectAdapter.notifyDataSetChanged()
        }

        return binding.root
    }


    override fun onDestroyView() {
        super.onDestroyView()
    }

}